package com.example.buxiaorui.studentsystem;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

public class DialogManager {
    private Dialog mDialog;
    private Context mContext;
    public DialogManager(Context context){
        mContext=context;
    }
    public void showError(){
        commoneDialog("密码不一致",R.drawable.error);
    }
    public void showAddsuccess(){
        commoneDialog("添加成功",R.drawable.success);
    }
    public void showAddfail(){
        commoneDialog("添加失败",R.drawable.error);
    }
    public void showUserexist(){
        commoneDialog("用户已存在",R.drawable.error);
    }
    public void showErrorpassword(){
        commoneDialog("密码格式错误",R.drawable.error);
    }
    public void showErrorCode(){
        commoneDialog("邀请码不存在",R.drawable.error);
    }
    public void showErrorUser(){
        commoneDialog("用户不存在",R.drawable.error);
    }
    public void showErrorLogin(){
        commoneDialog("密码错误",R.drawable.error);
    }
    public void showErrorRegist(){
        commoneDialog("注册失败",R.drawable.error);
    }public void showNotNull(){
        commoneDialog("信息不能为空",R.drawable.error);
    }
    public void commoneDialog(String s,int resource) {
        mDialog = new Dialog(mContext, R.style.mdialog);
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.dialog_custom, null);
        TextView textView = (TextView) view.findViewById(R.id.dialog_tips);
        textView.setText(s);
        ImageView imageView=(ImageView)view.findViewById(R.id.dialog_pic);
        imageView.setImageResource(resource);
        mDialog.setContentView(view);
        //获取dialog窗体，更改位置
        Window window = mDialog.getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        window.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
        layoutParams.y = 5;
        window.setAttributes(layoutParams);
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                mDialog.dismiss();
            }
        }, 2000);
        mDialog.setCanceledOnTouchOutside(true);
        mDialog.show();
    }
}
